package com.study.zzedu.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

/**
 * Created by kiros on 2020/9/3.
 */

public class ProgressBarView extends ProgressBar {
    public String text;
    private Paint mPaint;

    public ProgressBarView(Context context) {
        super(context);
        initText();
    }

    public ProgressBarView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initText();
    }


    public ProgressBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initText();
    }

    @Override
    public synchronized void setProgress(int progress) {
//        setText(progress);
        super.setProgress(progress);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Rect rect = new Rect();
        this.mPaint.getTextBounds(this.text, 0, this.text.length(), rect);
        int x = (getWidth() / 2) - rect.centerX();
        int y = (getHeight() / 2) - rect.centerY();
        int width = rect.width();

        //进度
        float radio = getProgress() * 1.0f / getMax();
        float mRealWidth = getWidth() * getProgress() / 100;
        float progressPosX = (int) (mRealWidth * radio);

        //结束点
        float textWidth = mPaint.measureText(this.text);
        float textEnd = (getWidth() + textWidth)/2;
        if (getProgress() > textEnd*100/getWidth()) {
            this.mPaint.setColor(Color.WHITE);
        } else {
            this.mPaint.setColor(Color.BLACK);
        }
        canvas.drawText(this.text, x, y, this.mPaint);
    }

    //初始化，画笔
    private void initText() {
        this.mPaint = new Paint();
        this.mPaint.setColor(Color.RED);
        this.mPaint.setTextSize(30.0f);
    }

//    private void setText() {
//        setText(this.getProgress());
//    }

    //设置文字内容
    public void setText(int progress, int num) {
//        int i = (progress * 100) / this.getMax();
//        this.text = String.valueOf(i) + "%";
        this.text = progress + "/" + num;
    }

    public void setText(String progress, String num) {
//        int i = (progress * 100) / this.getMax();
//        this.text = String.valueOf(i) + "%";
        this.text = progress + "/" + num;
    }
}
